using System;
using System.Xml;

namespace ScenarioFramework.Modules.DM.Application
{
	public class EmebeddedFileHelper
	{
		XmlDocument MyData = null;

		public EmebeddedFileHelper(string sXMLData)
		{
			MyData = new XmlDocument();
			try
			{
				MyData.LoadXml(sXMLData);
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (2, err.Message );
			}
		}

		public string ReadFromText(string txtField)
		{
			return EmebeddedFileHelper.ReadXMLDataFromText(MyData, txtField);
		}
		public string ReadFromGrid(string txtField,int RowNum, int ColNum)
		{
			return EmebeddedFileHelper.ReadXMLDataFromGrid(MyData, txtField, RowNum, ColNum);
		}

		public void WriteFromText(string txtField, string sValue)
		{
			EmebeddedFileHelper.WriteXMLDataFromText(MyData, txtField, sValue);
		}
		public void WriteFromGrid(string txtField,int RowNum, int ColNum, string sValue)
		{
			EmebeddedFileHelper.WriteXMLDataFromGrid(MyData, txtField, RowNum, ColNum,sValue);
		}

		public string GetXML()
		{
			return MyData.InnerXml;
		}

		public static string ReadXMLDataFromText(XmlDocument MyModule,string txtField)
		{
			if ( txtField == "" )
			{
				throw new Exception.DocumentManagerException (1, "Invalid Parameter" );
			}

			try
			{
				// variabli per l'interpretazione del XmlData
				XmlNode ModuleElement=MyModule.SelectSingleNode("Module/Fields/" + txtField);

				string Answer = ModuleElement.InnerText;
				return Answer;
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (3, err.Message );
			}
		}

		public static string ReadXMLDataFromGrid(XmlDocument MyModule, string txtField, int RowNum, int ColNum)
		{
			if ( txtField == "" )
			{
				throw new Exception.DocumentManagerException (1, "Invalid Parameter" );
			}
			
			try
			{
				XmlNode ModuleElement=MyModule.SelectSingleNode("Module/Matrixes/" + txtField + "/Row" + Convert.ToString(RowNum) + "/Col" + Convert.ToString(ColNum));
				string Answer = ModuleElement.InnerText;
				return Answer;
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (3, err.Message );
			}
		}

		public static string WriteXMLDataFromText(XmlDocument MyModule,string txtField, string Value)
		{
			if ( txtField == "" )
			{
				throw new Exception.DocumentManagerException (1, "Invalid Parameter" );
			}

			try
			{
				XmlNode ModuleElement=MyModule.SelectSingleNode("Module/Fields/" + txtField);
				ModuleElement.InnerText = Value;
				return MyModule.InnerXml;
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (3, err.Message );
			}
		}

		public static string WriteXMLDataFromGrid(XmlDocument MyModule, string txtField, int RowNum, int ColNum, string Value)
		{
			if ( txtField == "" )
			{
				throw new Exception.DocumentManagerException (1, "Invalid Parameter" );
			}

			try
			{
				XmlNode ModuleElement=MyModule.SelectSingleNode("Module/Matrixes/" + txtField + "/Row" + Convert.ToString(RowNum) + "/Col" + Convert.ToString(ColNum));
				ModuleElement.InnerText = Value;
				return MyModule.InnerXml;
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (3, err.Message );
			}
		}



		#region Read and Write a field inside XML Data.
		//1 - Invalid Parameter
		//2 - XMLData not well formed.
		//3 - Field not found.
		public static string WriteXMLData(string txtField, string XMLData, bool IsMatrix, int RowNum, int ColNum, string Value)
		{
			if ( txtField == "" || XMLData == "")
			{
				throw new Exception.DocumentManagerException (1, "Invalid Parameter" );
			}

			XmlDocument MyData = new XmlDocument();
			try
			{
				MyData.LoadXml(XMLData);
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (2, err.Message );
			}

			// variabli per l'interpretazione del XmlData
			XmlNode ModuleElement;

			if ( IsMatrix)
			{
				try
				{
					ModuleElement=MyData.SelectSingleNode("Module/Matrixes/" + txtField + "/Row" + Convert.ToString(RowNum) + "/Col" + Convert.ToString(ColNum));
					ModuleElement.InnerText = Value;
					return MyData.InnerXml;
				}
				catch (System.Exception err)
				{
					throw new Exception.DocumentManagerException (3, err.Message );
				}
			}
			else
			{
				try
				{
					ModuleElement=MyData.SelectSingleNode("Module/Fields/" + txtField);
					ModuleElement.InnerText = Value;
					return MyData.InnerXml;
				}
				catch (System.Exception err)
				{
					throw new Exception.DocumentManagerException (3, err.Message );
				}
			}
		}

		public static void GetNrRowAndCol(string sTxtField, string sXMLData, ref int iRow, ref int iCol)
		{
			// devo caricare la stringa xml in un reader per leggerla 
			XmlDocument MyModule = new XmlDocument();
					
			try
			{
				MyModule.LoadXml(sXMLData);
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (2, err.Message );
				
			}

			XmlNode ModuleElement = null;

			try
			{
				ModuleElement=MyModule.SelectSingleNode("Module/Matrixes/" + sTxtField + "[@RowNum]");
				string sItem = ModuleElement.InnerText;
				iRow = Int32.Parse (sItem);

				ModuleElement=MyModule.SelectSingleNode("Module/Matrixes/" + sTxtField + "@ColNum");
				sItem = ModuleElement.InnerText;
				iCol = Int32.Parse (sItem);
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (3, err.Message );
			} 
		}

		public static string ReadXMLData(string txtField, string sXMLData, bool IsMatrix, int RowNum, int ColNum)
		{
			if ( txtField == "" || sXMLData == "")
			{
				throw new Exception.DocumentManagerException (1, "Invalid Parameter" );
			}
				
			// devo caricare la stringa xml in un reader per leggerla 
			XmlDocument MyModule = new XmlDocument();
					
			try
			{
				MyModule.LoadXml(sXMLData);
			}
			catch (System.Exception err)
			{
				throw new Exception.DocumentManagerException (2, err.Message );
				
			}

			// variabli per l'interpretazione del XmlData
			XmlNode ModuleElement;

			if ( IsMatrix)
			{
				//cerco in myModule l'elemento e modifico il valore
				ModuleElement=MyModule.SelectSingleNode("Module/Matrixes/" + txtField + "/Row" + Convert.ToString(RowNum) + "/Col" + Convert.ToString(ColNum));

				try
				{
					string Answer = ModuleElement.InnerText;
					return Answer;
				}
				catch (System.Exception err)
				{
					throw new Exception.DocumentManagerException (3, err.Message );
				}
			}
			else
			{
				//cerco in myModule l'elemento e modifico il valore
				ModuleElement=MyModule.SelectSingleNode("Module/Fields/" + txtField);

				try
				{
					string Answer = ModuleElement.InnerText;
					return Answer;
				}
				catch (System.Exception err)
				{
					throw new Exception.DocumentManagerException (3, err.Message );
				}
			}
		}

		#endregion
	}
}
